package edu.kit.ksri.db.mturk.utils.numberevaluationhelpers;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 * 
 * ---------------------------------------------------------------------------
 * 
 * Purpose: Represents an interval
 */
public class Interval extends AbstractInterval {

	private double left;
	private double right;
	private boolean leftOpen;
	private boolean rightOpen;

	/**
	 * @param left
	 *            left border value
	 * @param right
	 *            right border value
	 * @param leftOpen
	 *            is the value left included (if yes: false)
	 * @param rightOpen
	 *            is the value right included (if yes: false)
	 */
	public Interval(double left, double right, boolean leftOpen,
			boolean rightOpen) {
		super();
		this.left = left;
		this.right = right;
		this.leftOpen = leftOpen;
		this.rightOpen = rightOpen;
	}

	@Override
	public boolean isInInterval(double value) {
		if (leftOpen && rightOpen)
			return value > left && value < right;
		if (!leftOpen && rightOpen)
			return value >= left && value < right;
		if (leftOpen && !rightOpen)
			return value > left && value <= right;
		if (!leftOpen && !rightOpen)
			return value >= left && value <= right;

		throw new RuntimeException("Fatal error for value " + value
				+ " in class Interval" + " (PercentageEvaluation.java), "
				+ "method isInInterval(double value)");
	}

	@Override
	public String toString() {
		String s = "";
		if (leftOpen)
			s += "(";
		else
			s += "[";
		s += left + ";" + right;
		if (rightOpen)
			s += ")";
		else
			s += "]";
		return s;
	}

}